import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;


/**
 * Copyright © 2018胡建松. All rights reserved.
 *
 * @Package: PACKAGE_NAME
 * @author: hujiansong
 * @date: 2018/7/24 9:53
 * @version: V1.0
 */
public class MainUi extends JFrame implements ActionListener {

    private JPanel jPanel1;
    private JButton fileButton;
    private JLabel filePath;
    private JButton catFile;

    private JFileChooser chooser = new JFileChooser();


    private JButton replaceButton = new JButton();

    private static final String FILE_SUBFIX = ".txt";

    public MainUi() {
        //初始化所有空间
        initView();
        // 初始化父控件
        initContiner();

        //初始化事件
        initEvent();
    }

    private void initEvent() {
        fileButton.addActionListener(this);
        replaceButton.addActionListener(this);
        catFile.addActionListener(this);
    }

    private void initContiner() {
        this.add(jPanel1);

        this.setLayout(new FlowLayout(FlowLayout.LEFT));
        this.setLayout(new GridLayout(3, 1));
        this.setTitle("字符串转换");
        this.setSize(350, 400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocation(500, 300);
        this.setVisible(true);
        this.setResizable(true);
        this.setResizable(false);
    }

    private void initView() {
        jPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        fileButton = new JButton("点击选择文件");
        filePath = new JLabel("");

        replaceButton = new JButton("确认替换");

        catFile = new JButton("查看文件");

        jPanel1.add(fileButton);
        jPanel1.add(filePath);

        jPanel1.add(replaceButton);
        jPanel1.add(catFile);
    }


    private void showBox(String msg) {
        JOptionPane.showMessageDialog(this, msg);
    }

    String outFilePath = null;
    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == fileButton) {
            String path = choosePath(0);
            if (path == null) {
                showBox("请先选择文件！");
                return;
            }

            if (!path.contains(FILE_SUBFIX)) {
                showBox("请先选择txt文件！");
                return;
            }
            filePath.setText(path.toString());
        } else if (e.getSource() == replaceButton) {
            //确认替换
            //先选择文件夹
            String dictory = choosePath(1);
            if (dictory == null) {
                showBox("请先选择替换文件存放文件夹！");
            }
            outFilePath = replaceFile(filePath.getText(), dictory);
        } else if (e.getSource() == catFile) {
            if (outFilePath == null) {
                showBox("请先完成替换操作！");
            }
            try {
                FileUtils.useCMDCommand(outFilePath);
            } catch (IOException e1) {
                e1.printStackTrace();
                showBox("打开文件失败！");
            }
        }
    }

    private String choosePath(int mode) {
        chooser.setFileSelectionMode(mode);
        int status = chooser.showOpenDialog(null);
        if (status == 1) {
            //
            showBox("请重新选择！");
            return null;
        } else {
            File file = chooser.getSelectedFile();
            return file.getAbsolutePath();
        }
    }

    private String replaceFile(String filePath, String dictory) {
        //
        String path = null;
        try {
            path = FileUtils.readFromFile(filePath, dictory);
        } catch (FileNotFoundException e) {
            showBox(e.getMessage() + "");
        } catch (IOException e2) {
            e2.printStackTrace();
            showBox("文件操作失败！");
        }

        showBox("替换成功！");
        return path;

    }


}
